package com.jmb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.model.entity.Project;
import com.jmb.api.service.ProjectService;
import com.jmb.api.mapper.ProjectMapper;
import com.jmb.api.vo.project.ProjectVo;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
* @author jinbo
* @description 针对表【project】的数据库操作Service实现
* @createDate 2025-04-08 13:53:56
*/
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project>
    implements ProjectService{
    @Autowired
    private ProjectMapper projectMapper;
    @Override
    public void saveProject(ProjectVo projectVo) {
        // 自动校验通过后执行逻辑
        if (projectVo ==null){
            throw new LeaseException(ResultCodeEnum.WEB_API_parameter);
        }
        LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Project::getName,projectVo.getName());
        if (this.count(queryWrapper)>0){
            throw new LeaseException(ResultCodeEnum.WEB_API_PROJECT_NAME);
        }
        Project project = new Project();
        BeanUtils.copyProperties(projectVo,project);
        this.save(project);
    }

    @Override
    public void updateProject(ProjectVo projectVo) {
        LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Project::getName,projectVo.getName()).ne(Project::getProjectId,projectVo.getProjectId());
        if (this.count(queryWrapper)>0){
            throw new LeaseException(ResultCodeEnum.WEB_API_PROJECT_NAME);
        }
        Project project = new Project();
        BeanUtils.copyProperties(projectVo,project);
        LambdaUpdateWrapper<Project> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Project::getProjectId,projectVo.getProjectId());
        this.update(project,updateWrapper);
    }

    @Override
    public IPage<ProjectVo> getProjectList(IPage<ProjectVo> page, String name, String domain) {
        return projectMapper.selectProjectList(page, name, domain);
    }

    @Override
    public ProjectVo getProjectById(Integer projectId) {
        if (projectId ==null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        Project project = projectMapper.selectById(projectId);
        ProjectVo projectVo = new ProjectVo();
        BeanUtils.copyProperties(project,projectVo);
        return projectVo;
    }

    @Override
    public Long getProjectCount(Integer projectId) {
        if (projectId==null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Project::getProjectId,projectId);
        return this.count(queryWrapper);
    }

    @Override
    public Project getProject(Project project) {
        LambdaQueryWrapper<Project> projectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        projectLambdaQueryWrapper.eq(Project::getProjectId,project.getProjectId());
        return this.getOne(projectLambdaQueryWrapper);
    }
}




